<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
<!-- creator-clang-codemodel.qdoc -->
  <title>Parsing C++ Files with the Clang Code Model | Qt Creator Manual</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="index.html">Qt Creator Manual</a></td><td >Parsing C++ Files with the Clang Code Model</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="index.html">Qt Creator Manual 4.11.1</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
  <link rel="prev" href="creator-diff-editor.html" />
  <link rel="next" href="creator-finding-overview.html" />
<p class="naviNextPrevious headerNavi">
<a class="prevPage" href="creator-diff-editor.html">Comparing Files</a>
<span class="naviSeparator">  &#9702;  </span>
<a class="nextPage" href="creator-finding-overview.html">Finding</a>
</p><p/>
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#about-the-clang-code-model">About the Clang Code Model</a></li>
<li class="level1"><a href="#configuring-clang-code-model">Configuring Clang Code Model</a></li>
<li class="level1"><a href="#clang-checks">Clang Checks</a></li>
<li class="level1"><a href="#specifying-clang-code-model-settings-at-project-level">Specifying Clang Code Model Settings at Project Level</a></li>
<li class="level1"><a href="#using-compilation-databases">Using Compilation Databases</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Parsing C++ Files with the Clang Code Model</h1>
<span class="subtitle"></span>
<!-- $$$creator-clang-codemodel.html-description -->
<div class="descr"> <a name="details"></a>
<p>The <i>code model</i> is the part of an IDE that understands the language you are using to write your application. It is the framework that allows Qt Creator to provide the following services:</p>
<ul>
<li><a href="creator-completing-code.html">Code completion</a></li>
<li>Syntactic and <a href="creator-highlighting.html">semantic highlighting</a></li>
<li>Navigating in the code by using the <a href="creator-editor-locator.html">locator</a>, <a href="creator-coding-navigating.html#moving-to-symbol-definition-or-declaration">following symbols</a>, and so on</li>
<li>Inspecting code by using the <a href="creator-sidebar-views.html">class browser</a>, the <a href="creator-sidebar-views.html#viewing-defined-types-and-symbols">outline</a>, and so on</li>
<li>Diagnostics</li>
<li><a href="creator-help.html#viewing-function-tooltips">Tooltips</a></li>
<li><a href="creator-editor-refactoring.html#finding-symbols">Finding and renaming symbols</a></li>
<li><a href="creator-editor-refactoring.html#applying-refactoring-actions">Refactoring actions</a></li>
</ul>
<p>Qt Creator comes with a plugin that provides some of these services for C++ on top of <a href="http://clang.llvm.org/">Clang</a>.</p>
<a name="about-the-clang-code-model"></a>
<h2 id="about-the-clang-code-model">About the Clang Code Model</h2>
<p>The Clang project provides libraries for parsing C language family source files. The feedback you get through warning and error markers is the same as a compiler will give you, not an incomplete set or a close approximation, as when using the built-in Qt Creator code model. Clang focuses on detailed information for diagnostics, which is really useful if the code contains typos, for example.</p>
<p>Clang keeps up with the development of the C++ language. At the time of this writing, it supports C++98/03, C++11, C++14, C++17, C89, C99, Objective-C, and Objective-C++.</p>
<p>On the downside, for large projects using Clang as code model is slower than using the built-in code model. Clang does not need to generate object files, but it still needs to parse and analyze the source files. For small projects that only use STL, this is relatively fast. But for larger projects that include several files, processing a single file and all the included files can take a while.</p>
<p>The Clang code model plugin now provides some of the services that were previously provided by the built-in C/C++ code model. Currently, the following services are implemented:</p>
<ul>
<li>Code completion</li>
<li>Syntactic and semantic highlighting</li>
<li><a href="creator-clang-tools.html">Diagnostics</a></li>
<li>Outline of symbols</li>
<li>Tooltips</li>
<li>Renaming of local symbols</li>
</ul>
<p>To use the built-in code model instead, select <b>Help</b> &gt; <b>About Plugins</b> &gt; <b>C++</b>, and deselect the <b>ClangCodeModel</b> check box. The changes take effect after you restart Qt Creator.</p>
<p>You can configure Clang diagnostics either globally or separately for:</p>
<ul>
<li>Clang code model (globally or at project level)</li>
<li><a href="creator-clang-tools.html">Clang tools</a> (globally or at project level)</li>
</ul>
<a name="configuring-clang-code-model"></a>
<h2 id="configuring-clang-code-model">Configuring Clang Code Model</h2>
<p>To configure the Clang code model globally:</p>
<ol class="1" type="1"><li>Select <b>Tools</b> &gt; <b>Options</b> &gt; <b>C++</b> &gt; <b>Code Model</b>.<p class="centerAlign"><img src="images/qtcreator-clang-code-model-options.png" alt="" /></p></li>
<li>To instruct the code model to interpret ambiguous header files as C language files if you develop mainly using C, select the <b>Interpret ambiguous headers as C headers</b> check box.</li>
<li>To process precompiled headers, deselect the <b>Ignore precompiled headers</b> check box.</li>
<li>To avoid out-of-memory crashes caused by indexing huge source files that are typically auto-generated by scripts or code, the size of files to index is limited to 5MB by default. To adjust the limit, edit the value for the <b>Do not index files greater than</b> check box. To index all files, deselect the check box.</li>
<li>Select <b>Manage</b> to specify the Clang checks to perform.<p class="centerAlign"><img src="images/qtcreator-diagnostics-configuration.png" alt="" /></p></li>
</ol>
<a name="clang-checks"></a>
<h2 id="clang-checks">Clang Checks</h2>
<p>The predefined configurations perform the following Clang checks:</p>
<ul>
<li><b>Clang-only pedantic checks</b> uses the <code>-Wpendantic</code> option that performs checks as required by strict ISO C and ISO C++.</li>
<li><b>Clang-only checks for questionable constructs</b> combines the <code>-Wall</code> and <code>-Wextra</code> checks for easily avoidable questionable constructions and some additional issues.</li>
<li><b>Clang-only checks for almost everything</b> uses the <code>-Weverything</code> option with negative options to suppress some warnings.</li>
</ul>
<p>You can edit the predefined configurations to perform particular checks beginning with <code>-W</code>. Each of these checks also has a negative version that begins with <code>-Wno</code>.</p>
<p>Keep in mind that some options turn on other options. For more information, see <a href="https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html">Options to Request or Suppress Warnings</a> or the GCC or Clang manual pages.</p>
<a name="specifying-clang-code-model-settings-at-project-level"></a>
<h2 id="specifying-clang-code-model-settings-at-project-level">Specifying Clang Code Model Settings at Project Level</h2>
<p>You can specify Clang code model settings at project level by selecting <b>Projects</b> &gt; <b>Clang Code Model</b>.</p>
<p>In addition to configuring the diagnostics, you can select the <b>Enable MSVC-compliant template parsing</b> check box to parse templates in a MSVC-compliant way. This enables Clang to parse headers for example from Active Template Library (ATL) or Windows Runtime Library (WRL). However, using the relaxed and extended rules means that no highlighting or completion can be provided within template functions.</p>
<p class="centerAlign"><img src="images/qtcreator-clang-code-model-build-settings.png" alt="" /></p><a name="using-compilation-databases"></a>
<h2 id="using-compilation-databases">Using Compilation Databases</h2>
<p>The <a href="https://clang.llvm.org/docs/JSONCompilationDatabase.html">JSON compilation database format</a> specifies how to replay single builds independently of the build system.</p>
<p>A <i>compilation database</i> is basically a list of files and the compiler flags that are used to compile the files. The database is used to feed the code model with the necessary information for correctly parsing the code when you open a file for editing.</p>
<p>To generate a compilation database from the information that the code model has, select <b>Build</b> &gt; <b>Generate Compilation Database</b>.</p>
<p>You can add files, such as non-C files, to the project in <i>compile_database.json.files</i>.</p>
<p>You can use the experimental Compilation Database Project Manager to open the files in a compilation database with access to all the editing features provided by the Clang code model.</p>
<p>To switch between header and source files, select <b>Tools</b> &gt; <b>C++</b> &gt; <b>Switch Header/Source</b>.</p>
<p>You can specify custom build steps and run settings for compilation database projects in the <b>Projects</b> mode. For more information, see <a href="creator-build-settings.html#adding-custom-build-steps">Adding Custom Build Steps</a> and <a href="creator-run-settings.html">Specifying Run Settings</a>.</p>
<p>To enable the plugin, select <b>Help</b> &gt; <b>About Plugins</b> &gt; <b>Build Systems</b> &gt; <b>Compilation Database Project Manager</b>. Then restart Qt Creator to load the plugin.</p>
</div>
<!-- @@@creator-clang-codemodel.html -->
<p class="naviNextPrevious footerNavi">
<a class="prevPage" href="creator-diff-editor.html">Comparing Files</a>
<span class="naviSeparator">  &#9702;  </span>
<a class="nextPage" href="creator-finding-overview.html">Finding</a>
</p>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
